package com.komodo.island.gdx.dialog;

import java.util.Random;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.Input;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.screen.FactsScreen;
import com.komodo.island.gdx.screen.GameScreen;
import com.komodo.island.gdx.screen.Screen;

public class WinDialog extends Dialog {

	Button retry;
	Button mainMenu;
	Button forward;

	public Image bgImage;
	public Image text;
	public Image siKomo;
	public Image score;
	Image newHighscore;
	Image newLevel;
	Image newPiece;
	
	public FactsScreen facts;

	public WinDialog(float width, float height, boolean stretch, Screen screen,
			Texture texture,KomodoIsland komodoIsland) {
		super(width, height, stretch, screen, texture,komodoIsland);
		// TODO Auto-generated constructor stub
		
		ID = Input.DIALOG_WIN;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
		if (facts == null){
		facts= new FactsScreen(null, 1, 5000, screen.previousScreen);
		facts.init(screen.komodoIsland, komodoIsland.factTexture);
		}
		
		
		background = new Image(backgroundName, new TextureRegion(komodoIsland.menuTexture,480, 0, 480, 320));
		
		bgImage = new Image("bg", new TextureRegion(texture, 0, 0,
				351, 194));

		text = new Image ("text", new TextureRegion(texture,351+200,71+28, 151, 28));
		
		siKomo= new Image("komo", new TextureRegion(texture, 351+200+179, 0,
				100, 102));
		score= new Image("score", new TextureRegion(texture, 300, 194,
				47, 16));
		newHighscore= new Image("new highscore", new TextureRegion(texture, 0, 214,
				279, 28));
		
		newLevel= new Image("new level", new TextureRegion(texture, 351+200, 0,
				179, 71));
		newPiece= new Image("new piece", new TextureRegion(texture, 351,58*2+28,
				179, 71));
		
		
		
		mainMenu = new Button("main menu", new TextureRegion(texture,
				351 + 128, 58 * 2 + 28 * 2 + 32, 32, 32), new TextureRegion(texture,
				351 + 128 + 32, 58 * 2 + 28 * 2 + 32, 32, 32));
		mainMenu = new Button("main menu", new TextureRegion(texture,351+200+97,71+28*2+64, 32, 32), new TextureRegion(texture,351+200+97+32,71+28*2+64, 32,
				32));
		
//		help = new Button("help", new TextureRegion(texture,351+200+97,71+28*2+64+32, 32, 32), new TextureRegion(texture,351+200+97+32,71+28*2+64+32, 32,
//				32));
		
		retry = new Button("retry", new TextureRegion(texture,351+200,71+28*2+64+28, 32, 32), new TextureRegion(texture,351+200+32,71+28*2+64+28, 32,
				32));
		
		
		forward = new Button("forward", new TextureRegion(
				komodoIsland.buttonTexture, 90 + 63 + 64 * 2, 200, 32,
				32), new TextureRegion(komodoIsland.buttonTexture, 90
				+ 63 + 64 * 2 + 32, 200, 32, 32));
		
	}

	@Override
	public void addElements() {
		// TODO Auto-generated method stub
		addActor(background);
		addActor(bgImage);
		addActor(score);
		// addActor(newHighscore);
		// addActor(newLevel);
		// addActor(newPiece);
		// addActor(siKomo);
		addActor(text);
		addActor(retry);
		addActor(mainMenu);
		addActor(forward);
		
	}

	@Override
	public void setThePosition() {
		// TODO Auto-generated method stub

		bgImage.x = 240 - 351 / 2;
		bgImage.y = 160 - 194 / 2;

		score.x = bgImage.x + 32;
		score.y = bgImage.y + 5;

		newHighscore.x = 0;
		newHighscore.y = 2;

		newPiece.x = newHighscore.width;
		newPiece.y = 320- newLevel.height;

		newLevel.x = 0;
		newLevel.y = 320-newLevel.height;

		siKomo.x = bgImage.x + 28;
		siKomo.y = bgImage.y + 194 - siKomo.height - 14;

		text.x = bgImage.x + 28 + bgImage.width / 3 - 10;
		text.y = bgImage.y + 64 * 2 + 14;

		retry.x = bgImage.x + 128 + 32 - 16;
		retry.y = bgImage.y + 96;

		mainMenu.x = retry.x + retry.width + 32;
		mainMenu.y = retry.y;
		
		forward.x = mainMenu.x + mainMenu.width + 32;
		forward.y = mainMenu.y;
		
		

	}

	@Override
	public void setTheListener() {
		// TODO Auto-generated method stub
		retry.clickListener = new Button.ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				if (Persistence.sound)
					komodoIsland.dialogSound.play(1);
				((GameScreen) screen).retryGame();
			}
		};
		mainMenu.clickListener = new Button.ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				if (Persistence.sound)
					komodoIsland.dialogSound.play(1);
				((GameScreen) screen).exitScreen();
			}
		};
	
		forward.clickListener = new Button.ClickListener() {

			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
//				komodoIsland.goToScreen(f)
				if (Persistence.sound)
					komodoIsland.dialogSound.play(1);
				screen.exitScreen();
				facts.setFact(new Random().nextInt(7));
				komodoIsland.goToScreen(facts);
			}
		};
				
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		super.draw();
		Art.drawString(getSpriteBatch(), ((GameScreen) screen).score + "",
				(int) (score.x + score.width + 1), (int) bgImage.y);
	}
	
	public void showUnlockLevel(){
		addActor(newLevel);
	}
	
	public void showNewPiece(){
		addActor(newPiece);
	}
	
	public void showHighscore(){
		addActor(newHighscore);
	}
	
	public void hideAllNotif (){
		removeActor(newHighscore);
		removeActor(newLevel);
		removeActor(newPiece);
	}
	
	

}
